# ---> 这是K-D树算法的临时版本。将与原始LDC代码结合使用
from tokenize import Double
import numpy as np
from sklearn.neighbors import KDTree
import time

def kd_Tree(X, D, N, n, point, Du, U):

    if(D!=len(X[0])):
        print("ERROR: The Input pointcloud_coordinates.csv file contain points of dimension (D) = ", len(X[0]))
        print("Quit!!")
        quit()

    N = int(input("Enter the total number of points (N): "))
    if(N!=len(X)):
        print("ERROR: Total number of points in input pointcloud_coordinates.csv file (N) = ", len(X))
        print("Quit!!")
        quit()
    print("Reading the data from pointcloud_coordinates.csv file ...")



    n = int(input("Enter the number of neighbour points (n): "))

    point = list(map(float, input("Enter the point of interest (x): ").split()))
    if(D!=len(point)): 
        print("ERROR: Point must be of dimension = ", D)
        print("Quit!!")
        quit()

    start = time.time()

    tree = KDTree(X)
    dist, ind = tree.query([point], k=n) 

    end = time.time() 

    print("\nNN Point Coordinates  ->  Distance from ", point)
    for i in range(0,n):
        print(X[ind[0][i]], "  ->  ", dist[0][i])

    print(f"程序查找邻居所用的时间（秒）: {end - start}\n")

    weigth_arr = np.zeros(n, dtype = float)
    for i in range(0,n):
        if(dist[0][i]!=0):
            weigth_arr[i] = 1/(dist[0][i]**2)


    if(D!=len(X[0])):
        print("错误：输入点cloud_coordes。csv文件包含维度点(D) = ", len(X[0]))
        print("退出!!")
        quit()

    N = int(input("输入总点数 (N): "))
    if(N!=len(X)):
        print("错误：输入pointcloud_cordinates中的点总数。csv文件 (N) = ", len(X))
        print("退出!!")
        quit()
    print("从pointcloud_coords读取数据。csv文件 ...")

  

    n = int(input("Enter the number of neighbour points (n): "))

    point = list(map(float, input("Enter the point of interest (x): ").split()))
    if(D!=len(point)): 
        print("ERROR: Point must be of dimension = ", D)
        print("Quit!!")
        quit()

    start = time.time()

    tree = KDTree(X)
    dist, ind = tree.query([point], k=n) 


    print("Reading the data from pointcloud_u_vector.csv file ...")

    interpolated_ux_arr = np.zeros(Du, dtype = float)

    if(Du==1):
        interpolated_ux = 0
        ux_numer = 0
        ux_denom = 0
        flag_val = 0 
        for i in range(0,n):
            if(dist[0][i]==0):
                interpolated_ux = U[ind[0][i]]
                flag_val = 1
                break
            else:
                ux_numer = ux_numer + (weigth_arr[i]*U[ind[0][i]])
                ux_denom = ux_denom + weigth_arr[i]
        
        if(flag_val==1):
            print("Interpolated u(x) = ", interpolated_ux)
        elif(flag_val==0 and ux_denom!=0):
            print("Interpolated u(x) = ", ux_numer/ux_denom)

    else:
        for j in range(0,Du):
            ux_numer = 0
            ux_denom = 0
            flag_val = 0
            for i in range(0,n):
                if(dist[0][i]==0):
                    interpolated_ux_arr[j] = U[ind[0][i]][j]
                    flag_val = 1
                    break
                else:
                    ux_numer = ux_numer + (weigth_arr[i]*U[ind[0][i]][j])
                    ux_denom = ux_denom + weigth_arr[i]
                    
            if(flag_val==0 and ux_denom!=0):
                interpolated_ux_arr[j] = ux_numer/ux_denom

            print("Interpolated u(x) component", j+1, " = ", interpolated_ux_arr[j])

    print()

